<?php

	class FragmentVO
	{
		var $id;
		var $content;
		var $data;
		
		public function __construct($id,$content,$data = null)
		{
			$this->id = $id;
			$this->content = $content;
			$this->data = $data;
		}
		
		public function isModuleFile()
		{return true;
			//return strpos($this->content,"/") > 0 && strpos(strtolower($this->content),"component") > 0);
		}
	}

	class EbView
	{
		static public $theme;
		static public $skin;
		
		static public $listFragments;
		
		static private $htmlPage;
		
		function __construct()
		{
			self::$listFragments = array();
		}
		
		static public function init()
		{		
			self::$skin = EbApplication::getConfig('defaultskin','default');
			self::$theme = EbApplication::getConfig('defaulttheme','default');
			
			if (EbApplication::$debug)
			{
				self::setFragment("debug","Core/DebugComponent");
			}
		}
		
		static public function getHtmlPage()
		{
			if (is_null(self::$htmlPage))
			{
				self::$htmlPage = new EbHtmlPage(EbApplication::getConfig('title','Default Title')); 
			}
			
			return self::$htmlPage;
		}
		
		static public function getThemeUrl()
		{
			return '/themes/'.self::$theme;
		}
		
		static public function widget($name,$data,$echo)
		{			
			$name = strtoupper($name[0]).substr($name,1);
			
			include_once(EBUILDY_PATH.'widget/'.$name.'Widget.php');
			
			$reflectionClass = new ReflectionClass($name.'Widget');
			$instance = $reflectionClass->newInstance();
			
			$instance->init($echo,$data);
			$instance->render();
		}
				
		static public function setFragment($id,$component,$data = null)
		{
			self::$listFragments[$id] = new FragmentVO($id,$component,$data);
		}
		
		static public function includeFragment($id)
		{
			$buffer = self::$listFragments;
			
			if (isset($buffer[$id]))
			{
				if (!$buffer[$id]->isModuleFile())
				{
					echo $buffer[$id]->content;
				}
				else
				{
					$content = explode('/',$buffer[$id]->content);
					
					self::includeComponent($content[0],$content[1]);
				}
				
				return true;
			}
			else
			{
				if (EbApplication::$debug)
				{
					echo "Debug message: Fragment $id is undefined!";
				}
				
				return false;
			}
		}
		
		static public function includeComponent($module,$name,$data = null)
		{			
			$module = ucfirst($module);
			$name = ucfirst($name);
			$path = MODULES_PATH.$module.'/v/'.$name;
			
			echo '<div class="eb-view-component">';
			
			if (strpos($path,'Component') === false)
			{
				$path .= 'Component';
			}
						
			if (file_exists($path.'.php'))
			{
				include($path.'.php');
			}
			elseif (file_exists($path.'.eby.php'))
			{
				EbTemplate::includeTemplate($path.'.eby.php');
			}
			elseif (file_exists($path.'.cfm'))
			{
				EbTemplate::includeTemplate($path.'.cfm');
			}
			else
			{
				echo "<b>The View Component $module / $name doesnt exists!</b>";
			}
			
			echo "</div>";
		}
					
		static public function renderHtml()
		{		
			ob_start();
						
			if (!is_null(self::$skin) && strlen(self::$skin) > 0)
			{
				include (THEMES_PATH.self::$theme.'/'.self::$skin.'Skin.php');
			}
			else
			{
				eb_include_fragment('content');
			}
									
			$content = ob_get_clean();
						
			self::getHtmlPage()->body = $content;
			
			ob_start();
						
			self::getHtmlPage()->render();
									
			$content = ob_get_clean();			
									
			return $content;
		}	

		static public function beginHtmlHead()
		{
			ob_start();
		}
		
		static public function endHtmlHead()
		{			
			$content = ob_get_clean();
			
			self::htmlHead($content);
		}		
		
		static public function htmlHead($value)
		{
			self::getHtmlPage()->htmlHead($value);
		}
		
		static public $listIncludedJs = array();
		
		static public function includeThemeCss()
		{
			$args = func_get_args();
			$buffer = '';
			$theme = self::$theme;
			
			$debug = EbApplication::$debug;
			
			foreach ($args AS $style)
			{
				$buffer .= '<link href="'.THEMES_URL.$theme.'/'.$style.'.css" rel="stylesheet" type="text/css" media="screen" />';
				
				if (!is_null($debug))
				{
					$debug->addCss($style);
				}
			}
			
			self::htmlHead($buffer);
		}	
		
		static public function includeEbuildyCss()
		{
			$args = func_get_args();
			$buffer = '';
			
			foreach ($args AS $style)
			{
				$buffer .= '<link href="/ebuildy/static/'.$style.'.css" rel="stylesheet" type="text/css" media="screen" />';
			}
			
			self::htmlHead($buffer);
		}			
		
		static public function includeThemeJavascript()
		{
			$args = func_get_args();
			$buffer = '';
			$theme = self::$theme;
			
			foreach ($args AS $js)
			{
				$buffer .= '<script type="text/javascript" src="'.THEMES_URL.$theme.'/'.$js.'.js"></script>';
			}
			
			self::htmlHead($buffer);
		}

		static public function includeEbuildyJavascript()
		{
			$args = func_get_args();
			$buffer = '';
			
			foreach ($args AS $js)
			{
				$buffer .= '<script type="text/javascript" src="/ebuildy/static/js/'.$js.'.js"></script>';
			}
			
			self::htmlHead($buffer);
		}

		static private $flagJQueryIncluded = false;

		static public function includeJQueryPlugin()
		{
			$args = func_get_args();
			$buffer = '';
			
			if (!self::$flagJQueryIncluded)
			{
				$buffer = '<script type="text/javascript" src="/ebuildy/static/js/jq/jquery.min.js"></script>';
				
				self::$flagJQueryIncluded = true;
			}
			
			foreach ($args AS $js)
			{
				$buffer .= '<script type="text/javascript" src="/ebuildy/static/js/jq/jquery.'.$js.'.js"></script>';
			}
			
			self::htmlHead($buffer);
		}
	}